using System;
using System.Collections.Generic;
using System.Text;

namespace Bludz
{
    public delegate void EventDelegate(String cmd, object[] args);

    class MessageEvent
    {
        public event EventDelegate myEvent;

        public void register(EventDelegate del)
        {
            myEvent += del;
        }

        public void unregister(EventDelegate del)
        {
            myEvent -= del;
        }

        public void pull(String cmd, object[] args)
        {
            myEvent(cmd, args);
        }
    }

    class EventServer : Singleton<EventServer>
    {
        Dictionary<String, MessageEvent> events;

        public EventServer()
        {
            events = new Dictionary<string,MessageEvent>();
        }

        private bool hasEvent(String cmd)
        {
            return events.ContainsKey(cmd);
        }

        public void register(String cmd, EventDelegate del)
        {
            if (!hasEvent(cmd))
                events[cmd] = new MessageEvent();
            events[cmd].register(del);
        }

        public void unregister(String cmd, EventDelegate del)
        {
            if (hasEvent(cmd))
                events[cmd].unregister(del);
        }

        public void pull(String cmd, params object[] args)
        {
            if (hasEvent(cmd))
                events[cmd].pull(cmd, args);
        }
    }
}
